// ============================================
// Rust 学习项目主程序
// ============================================

// 声明所有教学模块
#[path = "01_variables.rs"]
mod variables;

#[path = "02_data_types.rs"]
mod data_types;

#[path = "03_functions.rs"]
mod functions;

#[path = "04_control_flow.rs"]
mod control_flow;

#[path = "05_ownership.rs"]
mod ownership;

#[path = "06_structs.rs"]
mod structs;

#[path = "07_enums_and_pattern_matching.rs"]
mod enums_and_pattern_matching;

#[path = "08_collections.rs"]
mod collections;

#[path = "09_modules.rs"]
mod modules;

#[path = "10_error_handling.rs"]
mod error_handling;

#[path = "11_generics.rs"]
mod generics;

#[path = "12_traits.rs"]
mod traits;

#[path = "13_lifetimes.rs"]
mod lifetimes;

#[path = "14_testing.rs"]
mod testing;

#[path = "15_io.rs"]
mod io_ops;

#[path = "16_iterators_and_closures.rs"]
mod iterators_and_closures;

#[path = "17_smart_pointers.rs"]
mod smart_pointers;

#[path = "18_concurrency.rs"]
mod concurrency;

#[path = "19_async_await.rs"]
mod async_await;

#[path = "20_oop.rs"]
mod oop;

#[path = "21_advanced_features.rs"]
mod advanced_features;

use std::io::{self, Write};

fn main() {
    println!("\n╔════════════════════════════════════════════════════╗");
    println!("║          欢迎来到 Rust 学习教程                    ║");
    println!("╚════════════════════════════════════════════════════╝\n");
    
    loop {
        print_menu();
        
        let choice = get_user_input();
        
        match choice.trim() {
            "0" => {
                println!("\n🎓 运行所有教程...\n");
                run_all_tutorials();
            }
            "1" => variables::main(),
            "2" => data_types::main(),
            "3" => functions::main(),
            "4" => control_flow::main(),
            "5" => ownership::main(),
            "6" => structs::main(),
            "7" => enums_and_pattern_matching::main(),
            "8" => collections::main(),
            "9" => modules::main(),
            "10" => error_handling::main(),
            "11" => generics::main(),
            "12" => traits::main(),
            "13" => lifetimes::main(),
            "14" => testing::main(),
            "15" => io_ops::main(),
            "16" => iterators_and_closures::main(),
            "17" => smart_pointers::main(),
            "18" => concurrency::main(),
            "19" => async_await::main(),
            "20" => oop::main(),
            "21" => advanced_features::main(),
            "q" | "Q" => {
                println!("\n👋 感谢使用 Rust 学习教程，再见！");
                break;
            }
            _ => println!("\n❌ 无效的选择，请重试。"),
        }
        
        if choice.trim() != "q" && choice.trim() != "Q" {
            println!("\n按回车键继续...");
            let _ = get_user_input();
        }
    }
}

fn print_menu() {
    println!("\n┌──────────────────────────────────────────────────┐");
    println!("│              📚 请选择要学习的主题                │");
    println!("├──────────────────────────────────────────────────┤");
    println!("│  0.  🎯 运行所有教程                             │");
    println!("│                                                  │");
    println!("│  === 基础篇 ===                                  │");
    println!("│  1.  📝 变量与可变性                             │");
    println!("│  2.  🔢 数据类型                                 │");
    println!("│  3.  ⚙️  函数                                     │");
    println!("│  4.  🔄 流程控制                                 │");
    println!("│  5.  🔒 所有权                                   │");
    println!("│  6.  🏗️  结构体                                   │");
    println!("│  7.  🎲 枚举和模式匹配                           │");
    println!("│  8.  📦 常见集合                                 │");
    println!("│  9.  📁 包和模块                                 │");
    println!("│  10. ⚠️  错误处理                                 │");
    println!("│  11. 🔤 泛型                                     │");
    println!("│  12. 🎭 Trait                                    │");
    println!("│  13. ⏱️  生命周期                                 │");
    println!("│                                                  │");
    println!("│  === 进阶篇 ===                                  │");
    println!("│  14. 🧪 自动化测试                               │");
    println!("│  15. 💾 输入/输出                                │");
    println!("│  16. 🔁 迭代器和闭包                             │");
    println!("│  17. 🎯 智能指针                                 │");
    println!("│  18. 🧵 并发编程                                 │");
    println!("│  19. ⚡ 异步编程 (Async/Await)                   │");
    println!("│  20. 🎨 面向对象编程                             │");
    println!("│  21. 🚀 高级特性                                 │");
    println!("│                                                  │");
    println!("│  Q.  🚪 退出                                     │");
    println!("└──────────────────────────────────────────────────┘");
    print!("\n请输入选项: ");
    io::stdout().flush().unwrap();
}

fn get_user_input() -> String {
    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .expect("读取输入失败");
    input
}

fn run_all_tutorials() {
    let tutorials = vec![
        ("01 - 变量与可变性", variables::main as fn()),
        ("02 - 数据类型", data_types::main as fn()),
        ("03 - 函数", functions::main as fn()),
        ("04 - 流程控制", control_flow::main as fn()),
        ("05 - 所有权", ownership::main as fn()),
        ("06 - 结构体", structs::main as fn()),
        ("07 - 枚举和模式匹配", enums_and_pattern_matching::main as fn()),
        ("08 - 常见集合", collections::main as fn()),
        ("09 - 包和模块", modules::main as fn()),
        ("10 - 错误处理", error_handling::main as fn()),
        ("11 - 泛型", generics::main as fn()),
        ("12 - Trait", traits::main as fn()),
        ("13 - 生命周期", lifetimes::main as fn()),
        ("14 - 自动化测试", testing::main as fn()),
        ("15 - 输入/输出", io_ops::main as fn()),
        ("16 - 迭代器和闭包", iterators_and_closures::main as fn()),
        ("17 - 智能指针", smart_pointers::main as fn()),
        ("18 - 并发编程", concurrency::main as fn()),
        ("19 - 异步编程", async_await::main as fn()),
        ("20 - 面向对象编程", oop::main as fn()),
        ("21 - 高级特性", advanced_features::main as fn()),
    ];
    
    for (i, (name, tutorial_fn)) in tutorials.iter().enumerate() {
        println!("\n{}", "=".repeat(70));
        println!("开始教程 {} / {}: {}", i + 1, tutorials.len(), name);
        println!("{}\n", "=".repeat(70));
        
        tutorial_fn();
        
        if i < tutorials.len() - 1 {
            println!("\n按回车键继续下一个教程...");
            let _ = get_user_input();
        }
    }
    
    println!("\n╔════════════════════════════════════════════════════╗");
    println!("║          🎉 恭喜！所有教程已完成！                 ║");
    println!("╚════════════════════════════════════════════════════╝");
}
